package com.example.longimageView;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.example.longimageView.bean.AttributesBean;
import com.example.longimageView.utils.BitMapUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: njb
 * @date: 2023/5/8 20:49
 * @desc:
 */
public class TestJsonActivity extends AppCompatActivity {
    private static final String TAG = "TestLog";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
    }

    private void initData() {
        JSONObject body = new JSONObject();

        JSONObject deviceObject = new JSONObject();
        try {
            deviceObject.put("address", "02");
            deviceObject.put("deviceType", "03");
            deviceObject.put("friendlyName", "空调");
            String mode = "Low";
            String speed = "2";
            String temperature = "16";
            AttributesBean acAttributesBean = new AttributesBean();
            acAttributesBean.setStatus("on");
            acAttributesBean.setTemperature(temperature);
            acAttributesBean.setFanSpeed(speed);
            acAttributesBean.setMode(mode);
            String attributes = new Gson().toJson(acAttributesBean);
            deviceObject.put("attributes", BitMapUtils.getReplace(attributes));
            LogUtils.d(TAG,"--设备信息为--" + BitMapUtils.getReplace(attributes));

            List<JSONObject> list = new ArrayList<>();
            list.add(deviceObject);
            LogUtils.d(TAG,"--设备上报数组为--"+ list.toString());
            String str = BitMapUtils.getReplace(list.toString());
            body.put("devices", str);
            LogUtils.d(TAG,"--设备上报信息为--" + str);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }
}
